# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>
# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Konrad Tegtmeier <konrad.tegtmeier+aur@gmail.com>

_pkgname=cli
pkgname=hcloud
pkgver=1.37.0
pkgrel=1
pkgdesc="CLI for Hetzner Cloud"
arch=('loong64' 'x86_64')
url="https://github.com/hetznercloud/cli"
license=('MIT')
optdepends=('bash-completion: tab auto-completion'
            'zsh: tab auto-completion')
makedepends=('go-pie')
depends=('glibc')
provides=('hcloud')
source=("$pkgname-$pkgver.tar.gz::https://github.com/hetznercloud/cli/archive/refs/tags/v${pkgver}.tar.gz")
sha512sums=('9115a60f1f95fc74a838859dd0851b7dab9ddeed65e1d0ac55d6011fad4098523df20735de9a3149789529ec6b83c581b087d13a52ae2829465cd29a3b6bd253')

build() {
  export CGO_ENABLED=1
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw"
  cd "cli-${pkgver}"
  go build \
    -ldflags "-linkmode=external -w -X github.com/hetznercloud/cli/internal/version.Version=${pkgver}" \
    github.com/hetznercloud/cli/cmd/hcloud
  chmod +x ./hcloud
}

check() {
  cd "cli-${pkgver}"
  # go test -v -x ./...
}

package() {
  cd "cli-${pkgver}"
  install -Dsm755 ./hcloud "${pkgdir}/usr/bin/hcloud"
  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  mkdir -p "$pkgdir"/usr/share/{bash-completion/completions,zsh/site-functions,fish/vendor_completions.d}
  ./hcloud completion bash > "${pkgdir}/usr/share/bash-completion/completions/hcloud"
  ./hcloud completion zsh > "${pkgdir}/usr/share/zsh/site-functions/_hcloud"
  ./hcloud completion fish > "${pkgdir}/usr/share/fish/vendor_completions.d/hcloud.fish"
}
